package com.myiam.idm.portal.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.NotEmpty;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;



import org.springframework.stereotype.Component;

import com.myiam.idm.core.model.Email;
import com.myiam.idm.core.model.EmailType;
import com.myiam.idm.core.model.Identity;
import com.myiam.idm.core.model.Role;
import com.myiam.idm.core.services.IdentityService;

@Component("identityManager")
public class IdentityManager implements Serializable {
	private static final Logger logger = LoggerFactory.getLogger(IdentityManager.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6299457136533263184L;

	/*
	 * services
	 */
	@Autowired
	private IdentityService identityService;
	
	/*
	 * work
	 */
	private TreeNode selectedNode;  
	private String searchString;
	private String header = "save";
	
	private String emailAdd;
	private String phoneAdd;
	/*
	 * state
	 */

	
	private Identity identity; 
	
	private List<Role> attributedRoles;

	/*
	 * constructors
	 */
	
	public IdentityManager(){
		identity= new Identity();
	}
	
	
	/*
	 * action
	 */
	
	public void search() {
		System.out.println("search");
		
	}
	

	public void save() {
		logger.debug("saving " + identity);
	
		identityService.create(identity);
		
		System.out.println("create");
		//return "success";
	}
	
	
	/*  
	 * ajax events
	 */

    public void onNodeSelect(NodeSelectEvent event) {
    	String node = event.getTreeNode().getData().toString();
    		
    	selectedNode = event.getTreeNode();
    	
		logger.debug("selected node " + node);
    	
    }
    

    /*
     * actions	
     */
    public void addEmail() {
    	logger.debug("add Email " + emailAdd);
    	identity.addEmail(new Email(emailAdd, EmailType.PRIVATE));
    }
    
    public void addPhone() {
    	logger.debug("add Phone" + phoneAdd);
    	
    }
    
    
    public void addRole() {
    	logger.debug("trying to add node " + selectedNode);
    	if (selectedNode == null) {
    		// trow some msg back
    	} else {
    		if (attributedRoles == null) {
    			attributedRoles = new ArrayList<Role>();
    		}
    		attributedRoles.add((Role) selectedNode.getData());  
    	}
    }
	
	
	
	/*
	 * getters and setters
	 */
	
	public String getSearchString() {
		return searchString;
	}


	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
		
	public String getHeader() {
		return header;
	}
	
	
	/*
	@NotNull
	@NotEmpty
	@Pattern(regexp = "[A-Za-z0-9]*", message = "must contain only letters and numbers")
	public String getEmployeeId() {
		return employeeId;
	}
	
	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}
	
	@NotNull
	@NotEmpty
	@Pattern(regexp = "[A-Za-z]*", message = "must contain only letters")
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	@NotNull
	@NotEmpty
	@Pattern(regexp = "[A-Za-z]*", message = "must contain only letters")
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
/*

	
	/*
	 * domme getters en setters
	 * 
	 */
	public List<Role> getAttributedRoles() {
		return attributedRoles;
	}


	public void setAttributedRoles(List<Role> attributedRoles) {
		this.attributedRoles = attributedRoles;
	}


	public Identity getIdentity() {
		return identity;
	}


	public void setIdentity(Identity identity) {
		this.identity = identity;
	}


	public String getEmailAdd() {
		return emailAdd;
	}


	public void setEmailAdd(String emailAdd) {
		this.emailAdd = emailAdd;
	}


	public String getPhoneAdd() {
		return phoneAdd;
	}


	public void setPhoneAdd(String phoneAdd) {
		this.phoneAdd = phoneAdd;
	}
	
	

}
